package edu.sctu.wangxiaoqiang.day20150628;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user25 on 2015/6/28.
 */
public class GetData {
    /**
    从网页获取数据并返回string
     */
  public String getDataFromUrl(String path, String pathurl,String pathcan) {
      String string = "";
      int num = 0;
      byte[] bytes = new byte[1024];
      try {
          URL url = new URL(pathurl);
          try {
              URLConnection uc = url.openConnection();
              uc.setDoOutput(true);
              uc.setDoInput(true);
              uc.connect();
              OutputStream out = uc.getOutputStream();
              out.write(pathcan.getBytes());
              out.close();
              InputStream in = uc.getInputStream();
              FileOutputStream fos = new FileOutputStream(path);
              while ((num = in.read(bytes)) != -1) {
                  fos.write(bytes, 0, num);
                  string = string + new String(bytes, 0, num,"utf-8");
              }

          } catch (IOException e1) {
              e1.printStackTrace();
          }
      } catch (MalformedURLException e1) {
          e1.printStackTrace();
      }
      return string;
  }
  }
